import 'package:flutter/material.dart';

class JSWBaseStatelessView {}

/*
* 横向占比的单个view
* */
class BaseViewItem extends StatefulWidget {
  BaseViewItem({Key? key, required this.child, required this.weight})
      : super(key: key);
  final int weight;
  final Widget child;

  @override
  _BaseViewState createState() => _BaseViewState();
}

class _BaseViewState extends State<BaseViewItem> {
  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: widget.child,
      flex: widget.weight,
    );
  }
}

/*
* 横向比例总体的view
* */
class JSWLine extends StatefulWidget {
  JSWLine(
      {Key? key,
      required this.children,
      this.padding,
      this.margin,
      this.height,
      this.onPressed,
      this.clickable = false,
      this.decoration,
      this.onLongPressed,
      this.alignment,
      this.normalColor = Colors.transparent,
      this.pressColor})
      : super(key: key);
  final List<Widget> children;
  final EdgeInsetsGeometry? padding;
  final EdgeInsetsGeometry? margin;
  final double? height;
  final bool clickable;
  final Decoration? decoration;
  final Alignment? alignment;
  VoidCallback? onPressed;
  VoidCallback? onLongPressed;
  Color normalColor;

  Color? pressColor = Colors.grey[300];

  @override
  _JSWLineState createState() => _JSWLineState();
}

class _JSWLineState extends State<JSWLine> {
  bool press = false;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    if (widget.clickable) {
      return _clickWidget();
    } else {
      return _noClickWidget();
    }
  }

  Widget _clickWidget() {
    return GestureDetector(
      child: _noClickWidget(),
      onTap: () {
        if (widget.onPressed != null) {
          widget.onPressed!();
        }
      },
      onLongPress: () {
        if (widget.onLongPressed != null) {
          widget.onLongPressed!();
        }
      },
      onTapDown: (details) {
        setState(() {
          press = true;
        });
      },
      onTapUp: (details) {
        //延时500毫秒执行
        Future.delayed(const Duration(milliseconds: 100), () {
          if (press) {
            setState(() {
              press = false;
            });
          }
        });
      },
      onTapCancel: () {
        //延时500毫秒执行
        Future.delayed(const Duration(milliseconds: 100), () {
          if (press) {
            setState(() {
              press = false;
            });
          }
        });
      },
    );
  }

  Widget _noClickWidget() {
    if (widget.decoration == null) {
      return Container(
          height: widget.height,
          width: double.infinity,
          margin: widget.margin,
          padding: widget.padding,
          alignment: widget.alignment,
          color: press ? widget.pressColor : widget.normalColor,
          child: Flex(direction: Axis.horizontal, children: widget.children));
    } else {
      return Container(
          decoration: widget.decoration,
          height: widget.height,
          width: double.infinity,
          margin: widget.margin,
          padding: widget.padding,
          alignment: widget.alignment,
          child: Flex(direction: Axis.horizontal, children: widget.children));
    }
  }
}

/*
* 纵向可滑动view
* */
class JSWBaseView extends StatefulWidget {
  const JSWBaseView(
      {Key? key,
      required this.body,
      this.head,
      this.headHeight = 50,
      required this.bottom,
      this.bodyHeight,
      this.bodyColor,
      this.noScroll = false})
      : super(key: key);
  final Widget body;
  final AppBar? head;
  final Widget bottom;
  final double? bodyHeight;
  final Color? bodyColor;
  final double headHeight;
  final bool noScroll;

  @override
  _JSWBaseViewState createState() => _JSWBaseViewState();
}

class _JSWBaseViewState extends State<JSWBaseView> {
  FocusNode blankNode = FocusNode();

  @override
  void dispose() {
    blankNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor:
            widget.bodyColor == null ? Colors.grey[50] : widget.bodyColor,
        appBar: widget.head != null
            ? PreferredSize(
                preferredSize: Size.fromHeight(widget.headHeight),
                child: widget.head!,
              )
            : null,
        bottomNavigationBar: widget.bottom,
        body: GestureDetector(
          onTap: () {
            FocusScope.of(context).requestFocus(blankNode);
          },
          child: Container(
            height: widget.bodyHeight,
            child: widget.noScroll
                ? widget.body
                : SingleChildScrollView(
                    physics: const BouncingScrollPhysics(),
                    child: widget.body,
                  ),
          ),
        ));
  }
}

/*
* 主页纵向可滑动view
* */
class JSWMainPage extends StatefulWidget {
  const JSWMainPage(
      {Key? key,
      required this.body,
      required this.head,
      this.headHeight = 50,
      required this.bottom,
      this.bodyHeight,
      this.bodyColor,
      this.noScroll = false})
      : super(key: key);
  final Widget body;
  final AppBar head;
  final Widget bottom;
  final double? bodyHeight;
  final Color? bodyColor;
  final double headHeight;
  final bool noScroll;

  @override
  _JSWMainPageState createState() => _JSWMainPageState();
}

class _JSWMainPageState extends State<JSWMainPage> {
  FocusNode blankNode = FocusNode();

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Scaffold(
          backgroundColor: Colors.transparent,
          appBar: PreferredSize(
            preferredSize: Size.fromHeight(widget.headHeight),
            child: widget.head,
          ),
          bottomNavigationBar: widget.bottom,
          body: GestureDetector(
            onTap: () {
              FocusScope.of(context).requestFocus(blankNode);
            },
            child: Container(
              color: widget.bodyColor ?? Colors.transparent,
              height: widget.bodyHeight,
              child: widget.noScroll
                  ? widget.body
                  : SingleChildScrollView(
                      child: widget.body,
                    ),
            ),
          )),
    );
  }
}

class EasyTouch extends StatefulWidget {
  const EasyTouch(
      {Key? key,
      required this.child,
      this.padding,
      this.margin,
      this.height,
      this.width,
      this.border,
      this.shape,
      this.borderRadius,
      this.onPressed,
      this.onLongPressed,
      this.alignment,
      this.normalColor = Colors.transparent,
      this.pressColor = Colors.transparent})
      : super(key: key);
  final Widget child;
  final EdgeInsetsGeometry? padding;
  final EdgeInsetsGeometry? margin;
  final double? height;
  final double? width;
  final BoxBorder? border;
  final BoxShape? shape;
  final BorderRadiusGeometry? borderRadius;

  final VoidCallback? onPressed;
  final VoidCallback? onLongPressed;
  final Color normalColor;

  final Color? pressColor;
  final Alignment? alignment;

  @override
  _EasyTouchState createState() => _EasyTouchState();
}

class _EasyTouchState extends State<EasyTouch> {
  bool press = false;
  late BoxDecoration decoration;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    if (widget.shape == null) {
      decoration = BoxDecoration(
        color: press ? widget.pressColor : widget.normalColor,
        border: widget.border,
        borderRadius: widget.borderRadius,
      );
    } else {
      decoration = BoxDecoration(
        color: press ? widget.pressColor : widget.normalColor,
        border: widget.border,
        shape: widget.shape!, //可以设置角度，BoxShape.circle直接圆形
        borderRadius: widget.borderRadius,
      );
    }
    return GestureDetector(
      child: Container(
          decoration: decoration,
          height: widget.height,
          width: widget.width,
          margin: widget.margin,
          padding: widget.padding,
          alignment: widget.alignment,
          child: widget.child),
      onTap: () {
        if (!press) {
          //已经是false了就直接返回
          if (widget.onPressed != null) {
            widget.onPressed!();
          }
        } else {
          //不是false就要等一下才返回
          Future.delayed(const Duration(milliseconds: 150), () {
            if (widget.onPressed != null) {
              widget.onPressed!();
            }
          });
        }
      },
      onLongPress: () {
        if (widget.onLongPressed != null) {
          widget.onLongPressed!();
        }
      },
      onTapDown: (details) {
        setState(() {
          press = true;
        });
      },
      onTapUp: (details) {
        //延时500毫秒执行
        Future.delayed(const Duration(milliseconds: 100), () {
          if (press) {
            setState(() {
              press = false;
            });
          }
        });
      },
      onTapCancel: () {
        //延时500毫秒执行
        Future.delayed(const Duration(milliseconds: 100), () {
          if (press) {
            setState(() {
              press = false;
            });
          }
        });
      },
    );
  }
}
